**/*****************************************************************************
*Purpose: Estimate binscatter of neighborhood Children of Gautreaux Participants 
********************************************************************************/ 

**********************************
******** Binscatter Fig **********
**********************************

foreach j in m2019_pctblack  prm2019_pctblack  {
	reghdfe `j' if estsample==1 & m2019_pctblack!=., absorb($fe) residual(r_`j')
	sum `j' if e(sample)==1
	replace r_`j'=r_`j'+r(mean)
}

foreach q in place_shwhite {
	reghdfe `q' if estsample==1 & m2019_pctblack!=., absorb($fe) residual(r_`q')
}


gen numpiks_nhood=estsample if r_m2019_pctblack!=.
replace prm2019_pctblack =. if m2019_pctblack==.




capture frame drop figplot
frame create figplot str35 sourcedata str35 outcome str35 group str35 spec beta se obs piks treatpik controlpik 

binscatter m2019_pctblack prm2019_pctblack place_shwhite if estsample==1 & m2019_pctblack!=., absorb(mitrct80) controls(i.intakeyear i.yrbirth) genxq(plotbins)
reghdfe prm2019_pctblack place_shwhite  if estsample==1 & m2019_pctblack!=., absorb($fe) cluster(hhid)

unique pik if e(sample)==1
local numpik `=r(sum)'

frame post figplot ("MAFARF") ("prm2019_pctblack") ("all") ("binplot_coef") (_b[place_shwhite]) (_se[place_shwhite]) (`numpik') (`numpik') (-99999) (-99999)
frame post figplot ("MAFARF") ("prm2019_pctblack") ("all") ("binplot_intercept") (_b[_cons]) (_se[_cons]) (`numpik') (`numpik') (-99999) (-99999)


reghdfe m2019_pctblack place_shwhite if estsample==1 & m2019_pctblack!=., absorb($fe) cluster(hhid)
unique pik if e(sample)==1
local numpik `=r(sum)'

frame post figplot ("MAFARF") ("prm2019_pctblack") ("all") ("binplot_coef") (_b[place_shwhite]) (_se[place_shwhite]) (`numpik') (`numpik') (-99999) (-99999)
frame post figplot ("MAFARF") ("prm2019_pctblack") ("all") ("binplot_intercept") (_b[_cons]) (_se[_cons]) (`numpik') (`numpik') (-99999) (-99999)


******************

preserve
keep if plotbins!=.
collapse (mean) r_m2019_pctblack r_prm2019_pctblack r_place_shwhite (max) estsample (sum) numpiks_nhood , by(plotbins)


foreach w in r_m2019_pctblack r_prm2019_pctblack   r_place_shwhite  {
forvalues j=1/18 {
	if "`w'"=="r_m2019_pctblack" | "`w'"=="r_prm2019_pctblack"   {
	sum numpiks_earn if plotbins==`j'
	local numpik `=r(mean)'
	local source "LEHD"
	}


	sum `w' if plotbins==`j' 
	frame post figplot ("`source'") ("`w'") ("quantile`j'") ("binplot") (r(mean)) (-99999) (`numpik') (`numpik') (-99999) (-99999)
	
}
}

frame figplot: save "${output}/figplot_nhood_results.dta", replace
frame figplot: export delimited using "${output}/figplot_nhood_results.csv", replace
restore 
